import json

import scrapy
from fake_headers import Headers
from ..items import CarItem

header = Headers(
    browser='chrome',
    os='win',
    headers=True
)


class GuaziSpider(scrapy.Spider):
    name = 'GuaziSpider'

    allowed_domains = ['guazi.com']
    url_format = 'https://mapi.guazi.com/car-source/carList/pcList?minor=&sourceType=&ec_buy_car_list_ab=&location_city=&district_id=&tag=-1&license_date=&auto_type=&driving_type=&gearbox=&road_haul=&air_displacement=&emission=&car_color=&guobie=&bright_spot_config=&seat=&fuel_type=&order=&priceRange=0,-1&tag_types=18&diff_city=&intention_options=&initialPriceRange=&monthlyPriceRange=&transfer_num=&car_year=&carid_qigangshu=&carid_jinqixingshi=&cheliangjibie=&page={}&pageSize=20&city_filter=123&city=123&guazi_city=123&qpres=&versionId=0.0.0.0&osv=Unknown&platfromSource=wap'

    # 为当前爬虫指定特定的pipeline进行处理
    # 300 优先级
    custom_settings = {
        "ITEM_PIPELINES": {"SpiderDemo.pipelines.GuaZiSpiderPipeline": 300},
    }

    font0_num_map = {
        "&#59854;": "0",
        "&#58397;": "1",
        "&#58928;": "2",
        "&#60146;": "3",
        "&#58149;": "4",
        "&#59537;": "5",
        "&#60492;": "6",
        "&#57808;": "7",
        "&#59246;": "8",
        "&#58670;": "9"
    }

    font1_num_map = {
        "&#59854;": "0",
        "&#58670;": "1",
        "&#59246;": "2",
        "&#59537;": "3",
        "&#57808;": "4",
        "&#60146;": "5",
        "&#60492;": "6",
        "&#58149;": "7",
        "&#58928;": "8",
        "&#58397;": "9"
    }

    font2_num_map = {
        "&#59854;": "0",
        "&#58397;": "1",
        "&#58670;": "2",
        "&#58928;": "3",
        "&#60492;": "4",
        "&#59246;": "5",
        "&#57808;": "6",
        "&#60146;": "7",
        "&#59537;": "8",
        "&#58149;": "9"
    }

    font3_num_map = {
        '&#59854;': '0',
        '&#59537;': '1',
        '&#58149;': '2',
        '&#58397;': '3',
        '&#58670;': '4',
        '&#58928;': '5',
        '&#60492;': '6',
        '&#59246;': '7',
        '&#57808;': '8',
        '&#60146;': '9'
    }

    font4_num_map = {
        "&#59854;": "0",
        "&#59246;": "1",
        "&#57808;": "2",
        "&#60146;": "3",
        "&#59537;": "4",
        "&#58149;": "5",
        "&#58397;": "6",
        "&#58670;": "7",
        "&#58928;": "8",
        "&#60492;": "9"
    }

    font_num_map_list = [font0_num_map, font1_num_map, font2_num_map, font3_num_map, font4_num_map]

    def decode_num(self, text: str, map):
        result = text
        for key in map:
            result = result.replace(key, map[key])
        return result

    def decode_num_with_font_list(self, text):
        value_list = []
        for map in self.font_num_map_list:
            value_list.append(self.decode_num(text, map))
        return value_list

    # 根据计算规则：
    # 1、首付价<一口价
    # 2、首付价/一口价 ~ 30%
    # 选择最合适的编码规则
    def predict_best_index(self, price_list, first_pay_list):
        list1 = [0, 1, 2, 3, 4]
        for i in range(5):
            price = float(price_list[i].split("万")[0])
            first_pay = 0
            if first_pay_list[i] != '':
                first_pay = float(first_pay_list[i].split("万")[0])
            percent = int(first_pay * 10 / price)
            if first_pay >= price or percent > 3:
                list1.remove(i)
        if len(list1) != 0:
            return list1[0]
        return 0

    def start_requests(self):
        for i in range(1, 50):
            url = self.url_format.format(i)
            yield scrapy.Request(url=url, headers=header.generate(), callback=self.parse)

    def parse(self, response):
        jsonObj = json.loads(response.text)
        data = jsonObj.get('data', None)
        if data is not None:
            postList = data.get('postList', None)
            if postList is not None:
                for car in postList:
                    title = car['title']
                    buyOutPrice = car.get('buyOutPrice', '0万')
                    # 爬取到的数据都是加密的 需要解密
                    # &#59246;.&#59537;&#58670;万
                    price = car['price']
                    first_pay = car['first_pay']
                    road_haul = car['road_haul']
                    de_price_list = self.decode_num_with_font_list(price)
                    de_first_pay_list = self.decode_num_with_font_list(first_pay)
                    de_road_haul_list = self.decode_num_with_font_list(road_haul)
                    index = self.predict_best_index(de_price_list, de_first_pay_list)
                    best_price = de_price_list[index]
                    best_first_pay = de_first_pay_list[index]
                    best_road_haul = de_road_haul_list[index]

                    car = CarItem()
                    car['title'] = title
                    car['best_price'] = best_price
                    car['best_first_pay'] = best_first_pay
                    car['best_road_haul'] = best_road_haul
                    car['buyOutPrice'] = buyOutPrice

                    yield car
